package util

import (
	"bytes"
	"sync"
)

var BufferPool = sync.Pool{
	New: func() interface{} {
		return new(bytes.Buffer)
	},
}

func Buffer() *bytes.Buffer {
	return BufferPool.Get().(*bytes.Buffer)
}

func PutBuffer(buf *bytes.Buffer) {
	BufferPool.Put(buf)
}

func BufferSlice(buf *bytes.Buffer, size int) []byte {
	buf.Reset()

	if buf.Cap() < size {
		buf.Grow(size)
	}

	return buf.Bytes()[:size]
}
